package com.healthcloud.service.impl;

import com.healthcloud.dao.TestResultMapper;
import com.healthcloud.entity.TestResult;
import com.healthcloud.service.TestResultService;
import com.healthcloud.utils.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class TestResultServiceImpl implements TestResultService {


    @Autowired
    private TestResultMapper testResultMapper;

    @Override
    public void saveBloodSugar(TestResult testresult) {
        testresult.setTestKindCode("105");
        testresult.setTestKindName("血糖");
        testresult.setDataType("20");
        testresult.setIsPublish(1);
        testresult.setCreateTime(new Date());

        if ("105001".equals(testresult.getTestItemCode())) {
            testresult.setTestItemName("空腹血糖");
        } else if ("105002".equals(testresult.getTestItemCode())) {
            testresult.setTestItemName("餐后2小时血糖");
        }
        testResultMapper.save(testresult);
    }

    @Override
    public void saveBloodPressure(String sn, String idCard, Double szyValue, Double ssyValue, Double heartRate, Date testTime) {
        TestResult entity = new TestResult();
        entity.setMkMedicalInstrumentId(sn);
        entity.setTestTime(testTime);
        entity.setIdCard(idCard);
        entity.setTestKindCode("104");
        entity.setTestKindName("血压");
        entity.setDataType("20");
        entity.setIsPublish(1);
        entity.setCreateTime(new Date());

        entity.setTestItemCode("104001");
        entity.setTestItemName("收缩压");
        entity.setTestValue(String.valueOf(ssyValue));
        entity.setValueUnit("mmHg");
        testResultMapper.save(entity);

        entity.setId(null);
        entity.setTestItemCode("104002");
        entity.setTestItemName("舒张压");
        entity.setValueUnit("mmHg");
        entity.setTestValue(String.valueOf(szyValue));
        testResultMapper.save(entity);

        entity.setId(null);
        entity.setTestItemCode("104003");
        entity.setTestItemName("周期脉搏");
        entity.setValueUnit("BPM");
        entity.setTestValue(String.valueOf(heartRate));
        testResultMapper.save(entity);
    }

    @Override
    public List<TestResult> findByIdCard(String idCard, PageParam pageParam) {
        return testResultMapper.findByIdCard(idCard,pageParam);
    }

    @Override
    public int findCountByIdCard(String idCard) {
        return testResultMapper.findCountByIdCard(idCard);
    }

    /**
     * 查询血压报表
     *
     * @param errorStatus 异常状态
     * @param reportTime  检测时间
     * @param idCard      身份证号
     * @param pageParam   分页
     * @return
     */
    @Override
    public List bpReport(Integer errorStatus, String reportTime, String idCard, PageParam pageParam) {
        return testResultMapper.bpReport(errorStatus, reportTime, idCard, pageParam);
    }

    /**
     * 血压检测次数
     *
     * @param errorStatus 异常状态
     * @param idCard      身份证号
     * @param reportTime
     * @return
     */
    @Override
    public Integer bpCount(Integer errorStatus, String idCard, String reportTime) {
        return testResultMapper.bpCount(errorStatus, reportTime, idCard);
    }

    /**
     * 查询血糖报表
     *
     * @param errorStatus 异常状态
     * @param reportTime  检测时间
     * @param idCard      身份证号
     * @param pageParam   分页
     * @return
     */
    @Override
    public List bsReport(Integer errorStatus, String reportTime, String idCard, PageParam pageParam) {
        return testResultMapper.bsReport(errorStatus, reportTime, idCard, pageParam);
    }

    /**
     * 查询血糖检测次数
     *
     * @param idCard      身份证号
     * @param errorStatus 异常状态
     * @param reportTime
     * @return
     */
    @Override
    public Integer bsCount(String idCard, Integer errorStatus, String reportTime) {
        return testResultMapper.bsCount(idCard, errorStatus, reportTime);
    }
}
